//
//  XuanZeKeShiDefaultOtherView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/6/3.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "XuanZeKeShiDefaultOtherView.h"

@implementation XuanZeKeShiDefaultOtherView

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubView];
        [self initOption];
        [self layout];
    }
    return self;
}
- (void)initSubView{
    [self addSubview:self.baseView];
    [_baseView addSubview:self.fuDaoCiShuView];
    [_baseView addSubview:self.fuDaoZhuTiView];
    [_baseView addSubview:self.selectedTime];
//    [_baseView addSubview:self.fuDaoNeiRongView];
   
}
- (void)initOption{
    
}
- (void)layout{
    [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(@10);
        make.bottom.equalTo(@0).offset(-10);
        make.width.equalTo(@350);
    }];
    [_fuDaoCiShuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(10);
        make.width.equalTo(@320);
        make.height.equalTo(@50);
    }];
    [_fuDaoZhuTiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self->_fuDaoCiShuView.mas_bottom).offset(10);
        make.width.equalTo(@320);
        make.height.equalTo(@50);
    }];
    [_selectedTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self->_fuDaoZhuTiView.mas_bottom).offset(10);
        make.width.equalTo(@320);
        make.height.equalTo(@50);
    }];
//    [_fuDaoNeiRongView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(self.mas_centerX);
//        make.top.equalTo(self->_selectedTime.mas_bottom).offset(10);
//        make.width.equalTo(@320);
//        make.height.equalTo(@80);
//    }];
}
- (UIView *)baseView{
    if (!_baseView) {
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 4;
        view.layer.masksToBounds = YES;
        view.layer.borderColor = LINCOLOR.CGColor;
        view.layer.borderWidth = 1;
        _baseView = view;
    }
    return _baseView;
}
- (XuanZeKeShiDefultView *)fuDaoCiShuView{
    if (!_fuDaoCiShuView) {
        XuanZeKeShiDefultView *banJiMingChengView = [[XuanZeKeShiDefultView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        banJiMingChengView.leftTitleLabel.text = @"班级次数:";
        banJiMingChengView.rightDetailTF.delegate = self;
        banJiMingChengView.rightDetailTF.enabled = NO;
        _fuDaoCiShuView = banJiMingChengView;
    }
    return _fuDaoCiShuView;
}
- (XuanZeKeShiDefultView *)fuDaoZhuTiView{
    if (!_fuDaoZhuTiView) {
        XuanZeKeShiDefultView *banJiMingChengView = [[XuanZeKeShiDefultView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        banJiMingChengView.leftTitleLabel.text = @"辅导主题:";
        banJiMingChengView.rightDetailTF.delegate = self;
        _fuDaoZhuTiView = banJiMingChengView;
    }
    return _fuDaoZhuTiView;
}
- (XuanZeKeShiEditeContentView *)fuDaoNeiRongView{
    if (!_fuDaoNeiRongView) {
        XuanZeKeShiEditeContentView *banJiMingChengView = [[XuanZeKeShiEditeContentView alloc]initWithFrame:CGRectMake(0, 0, 320, 80)];
        banJiMingChengView.leftTitleLabel.text = @"辅导内容:";
        banJiMingChengView.rightDataView.delegate = self;
        _fuDaoNeiRongView = banJiMingChengView;
    }
    return _fuDaoNeiRongView;
}
- (XuanZeShiJianView *)selectedTime{
    if (!_selectedTime) {
        XuanZeShiJianView *view = [[XuanZeShiJianView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
        view.jieZhiRiQiLabel.text = @"截止日期:";
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedJierShuTimeView)];
        [view.jierShuTimeView addGestureRecognizer:tap];
        UITapGestureRecognizer *tapK = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectedKaiShiTimeView)];
        [view.kaiShiTimeView addGestureRecognizer:tapK];
        _selectedTime  = view;
    }
    return _selectedTime;
}
- (void)selectedKaiShiTimeView{
    [DatePickerView setDatePickerViewWithFrame:self.frame andTitle:@"开始日期" andSuccessBlock:^(NSString * _Nonnull dateString) {
       // NSLog(@"选择日期 %@",dateString);
        //选择完成结束日期选择开始日期
        if (![self->_selectedTime.jierShuTimeView.nameLabel.text containsString:@"日期"]) {
            NSInteger index  = [NSObject compareOneDay:[NSObject dateFormatter:self->_selectedTime.kaiShiTimeView.nameLabel.text] withAnotherDay:[NSObject dateFormatter:dateString]];
            if (index == -1) {
                [XSInfoView showInfo:@"结束日期不能再开始日期之前" onSubView:self];
                return ;
            }
        }
        
        self->_selectedTime.kaiShiTimeView.nameLabel.text = dateString;
        self.startBlock(dateString,self.index);
    }];
}
- (void)selectedJierShuTimeView{
    
    if ([self->_selectedTime.kaiShiTimeView.nameLabel.text containsString:@"日期"]) {
        [XSInfoView showInfo:@"请选择开始日期" onSubView:self];
        return;
    }
    
    [DatePickerView setDatePickerViewWithFrame:self.frame andTitle:@"结束日期" andSuccessBlock:^(NSString * _Nonnull dateString) {
       // NSLog(@"选择日期 %@",dateString);
        
        NSInteger index  = [NSObject compareOneDay:[NSObject dateFormatter:dateString] withAnotherDay:[NSObject dateFormatter:self->_selectedTime.kaiShiTimeView.nameLabel.text]];
        if (index == -1) {
            [XSInfoView showInfo:@"结束日期不能再开始日期之前" onSubView:self];
            return ;
        }
        
        self->_selectedTime.jierShuTimeView.nameLabel.text = dateString;
        self.endBlock(dateString,self.index);
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField isEqual:_fuDaoCiShuView.rightDetailTF]) {
        //辅导次数编辑
        
    }else if ([textField isEqual:_fuDaoZhuTiView.rightDetailTF]){
        //辅导主题
        self.editeMainTitleBlock(textField.text,self.index);
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView{
    if ([textView isEqual:_fuDaoNeiRongView.rightDataView]) {
        //辅导内容
        self.editeContentClock(textView.text,self.index);
    }
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self endEditing:YES];
    return YES;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        [textView resignFirstResponder];
        return NO;
    }
    
    return YES;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
